package xx.iverson.file_manager.controller;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import xx.iverson.file_manager.entity.CategoryLevel1;
import xx.iverson.file_manager.entity.CategoryLevel2;
import xx.iverson.file_manager.entity.CategoryLevel2DTO;
import xx.iverson.file_manager.entity.FileInfo;
import xx.iverson.file_manager.mapper.SysUserMapper;
import xx.iverson.file_manager.service.CategoryLevel1Service;
import xx.iverson.file_manager.service.CategoryLevel2Service;
import xx.iverson.file_manager.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private CategoryLevel1Service categoryLevel1Service;

    @Autowired
    private CategoryLevel2Service categoryLevel2Service;

    @Autowired
    private FileInfoService fileService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @GetMapping
    public String fileManagement(Model model) {
        List<FileInfo> files = fileService.getAllFiles();
        List<CategoryLevel1> level1Categories = categoryLevel1Service.getAllLevel1WithLevel2();
        List<CategoryLevel2> level2Categories = categoryLevel2Service.getAllLevel2Categories();
        model.addAttribute("files", files);
        model.addAttribute("level1Categories", level1Categories);
        model.addAttribute("level2Categories", level2Categories);
        model.addAttribute("title", sysUserMapper.selectTitle());
        return "admin";
    }

    // 添加一级分类
    @PostMapping("/category/level1/add")
    public String addLevel1Category(@RequestParam String name,
                                    @RequestParam(defaultValue = "0") Integer sortOrder,
                                    RedirectAttributes redirectAttributes) {
        CategoryLevel1 category = new CategoryLevel1();
        category.setName(name);
        category.setSortOrder(sortOrder);
        boolean success = categoryLevel1Service.addLevel1Category(category);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "一级分类添加成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "一级分类名称已存在或添加失败");
        }
        return "redirect:/admin";
    }

    // 更新一级分类
    @PostMapping("/category/level1/update")
    public String updateLevel1Category(@RequestParam Integer id,
                                       @RequestParam String name,
                                       @RequestParam Integer sortOrder,
                                       RedirectAttributes redirectAttributes) {
        CategoryLevel1 category = new CategoryLevel1();
        category.setId(id);
        category.setName(name);
        category.setSortOrder(sortOrder);

        boolean success = categoryLevel1Service.updateLevel1Category(category);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "一级分类更新成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "一级分类名称已存在或更新失败");
        }
        return "redirect:/admin";
    }

    // 删除一级分类
    @PostMapping("/category/level1/delete")
    public String deleteLevel1Category(@RequestParam Integer id, RedirectAttributes redirectAttributes) {
        boolean success = categoryLevel1Service.deleteLevel1Category(id);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "一级分类删除成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "一级分类下有二级分类，无法删除");
        }
        return "redirect:/admin";
    }

    // 添加二级分类
    @PostMapping("/category/level2/add")
    public String addLevel2Category(CategoryLevel2DTO categoryDTO,
                                    RedirectAttributes redirectAttributes) {
        CategoryLevel2 category = new CategoryLevel2();
        category.setName(categoryDTO.getName());
        category.setLevel1Id(categoryDTO.getLevel1Id());
        category.setSortOrder(categoryDTO.getSortOrder());

        boolean success = categoryLevel2Service.addLevel2Category(category);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "二级分类添加成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "二级分类名称已存在或添加失败");
        }
        return "redirect:/admin";
    }

    // 更新二级分类
    @PostMapping("/category/level2/update")
    public String updateLevel2Category(CategoryLevel2DTO categoryDTO,
                                       RedirectAttributes redirectAttributes) {
        CategoryLevel2 category = new CategoryLevel2();
        category.setId(categoryDTO.getId());
        category.setName(categoryDTO.getName());
        category.setLevel1Id(categoryDTO.getLevel1Id());
        category.setSortOrder(categoryDTO.getSortOrder());

        boolean success = categoryLevel2Service.updateLevel2Category(category);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "二级分类更新成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "二级分类名称已存在或更新失败");
        }
        return "redirect:/admin";
    }

    // 删除二级分类
    @PostMapping("/category/level2/delete")
    public String deleteLevel2Category(@RequestParam Integer id, RedirectAttributes redirectAttributes) {
        boolean success = categoryLevel2Service.deleteLevel2Category(id);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "二级分类删除成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "二级分类下有文件，无法删除");
        }
        return "redirect:/admin";
    }
    @GetMapping("/category/level2/by-level1")
    @ResponseBody
    public List<CategoryLevel2> getLevel2CategoriesByLevel1(@RequestParam Integer level1Id) {
        return categoryLevel2Service.getLevel2CategoriesByLevel1Id(level1Id);
    }
    // 更新文件信息
    @PostMapping("/file/update")
    public String updateFile(@RequestParam Integer id,
                             @RequestParam String originalName,
                             @RequestParam(required = false) Integer categoryId,
                             @RequestParam String fileDescription,
                             RedirectAttributes redirectAttributes) {
        FileInfo fileInfo = fileService.getFileById(id);
        if (fileInfo != null) {
            fileInfo.setOriginalName(originalName);
            // 处理categoryId为空的情况
            if (categoryId != null && categoryId > 0) {
                fileInfo.setCategoryId(categoryId);
            } else {
                fileInfo.setCategoryId(null);
            }
            fileInfo.setFileDescription(fileDescription);
            boolean success = fileService.updateFile(fileInfo);
            if (success) {
                redirectAttributes.addFlashAttribute("message", "文件信息更新成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "文件信息更新失败");
            }
        } else {
            redirectAttributes.addFlashAttribute("error", "文件不存在");
        }
        return "redirect:/admin";
    }

    // 删除文件
    @PostMapping("/file/delete")
    public String deleteFile(@RequestParam Integer id, RedirectAttributes redirectAttributes) {
        boolean success = fileService.deleteFile(id);
        if (success) {
            redirectAttributes.addFlashAttribute("message", "文件删除成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "文件删除失败");
        }
        return "redirect:/admin";
    }

    @PostMapping("/modifyTitle")
    public String modifyTitle(@RequestParam String title, RedirectAttributes redirectAttributes) {
        int result = sysUserMapper.updateTitle(title);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("message", "网站标题更新成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "网站标题更新失败");
        }
        return "redirect:/admin";
    }
}